<?php
namespace Home\Controller;
use Think\Controller;

class IndexController extends Controller {
    //获取列表
    public function getArticleList(){    
      	$nowPage = I('post.nowPage', '');
        $count = I('post.count', '');
      	$typeId = I('post.type', '');
        $post = D('post');
      	$good = M('good');
        $look = M('look');
        $localIp = get_client_ip();
        $area = $this->geturl('http://ip.taobao.com/service/getIpInfo.php?ip='.$localIp);

        $date = date("Y-m-d H:i:s");

        if($typeId != "") {
            $post_type = M('post_type');
            $type = $post_type->where(array('type_id' => $typeId))->select();
            $result['articleList'] = $post->where(array('post_type' => $type[0]["type_name"]))->order('post_createtime desc')->page($nowPage, $count)->select();      
        }else {
        	$result['articleList'] = $post->order('post_createtime desc')->page($nowPage, $count)->select();
        }

        //遍历文章查找 当前用户有没有点赞和点赞总数
        $goodResult = $result['articleList'];
        $uid = session('uid');
        $length = count($goodResult);   
        for($i = 0; $i < $length; $i++) {
            $result['articleList'][$i]['goodCount'] = $good->where(array('post_id' => $goodResult[$i]['post_id']))->count();
            $len = $good->where(array('post_id' => $goodResult[$i]['post_id'], 'uid' => $uid))->count();
            $result['articleList'][$i]['isGood'] = $len > 0 ? 'good' : "bad";
        }

      	if($post.length < $count) {
      		$result['isMore'] = false;      		
      	}else {
      		$result['isMore'] = true;
        }
        $data = $area['data'];
        // var_dump($data); exit;
        $look->data(array(
            'look_ip'=> $localIp, 
            'look_time'=> $date, 
            'look_area'=> $data['area'],
            'look_country' => $data['country'],
            'look_region' => $data['region'],
            'look_isp' => $data['isp'],
            'look_city' => $data['city'],
            'look_county' => $data['county'],
        ))->add();
        $this->ajaxReturn($result);
    }

    function geturl($url){
        $headerArray =array("Content-type:application/json;","Accept:application/json");
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); 
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl,CURLOPT_HTTPHEADER,$headerArray);
        $output = curl_exec($ch);
        curl_close($ch);
        $output = json_decode($output,true);
        return $output;
    }

    //点赞
    public function good(){    
        $post_id = I('post.post_id', '');
        $good = M('good');
        $uid = session('uid');   
        if($uid == "")  {
            $result["type"] = null;
        }else {            
            $goodResult = $good->where(array('post_id' => $post_id, 'uid' => $uid))->select();            
            if(count($goodResult) < 1) {         
                $good->data(array('post_id' => $post_id, 'uid' => $uid))->add();  
                $result['type'] = 'good';
            }else {
                $good->where(array('good_id' => $goodResult[0]['good_id']))->delete();
                $result['type'] = 'bad';
            }
            $result['goodCount'] = $good->where(array('post_id' => $post_id))->count(); 
        }
        $this->ajaxReturn($result);
    }
}


